<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
		<title>教师界面</title>
		<style type="text/css">
			.table {
				margin: 15px;
				padding: 5px;
			}

			.td {
				margin-left: 20px;
				width: 100px;
			}

			.button {
				text-align: center;
				width: 100px;
				height: 50px;
				font-size: 20px;
				border-radius: 10px;
				cursor: pointer;
				background-color: #F19149;
			}
		</style>

		<script type="text/javascript">
			function editExam() {
				window.location = 'Exam_Edit.jsp';

			}
		</script>
	</head>
	<jsp:include page="Teacher_header.jsp"></jsp:include>
	<body>
		<form action="">

			<div class="button" onclick="editExam()">添加考试</div>
			<table class="table" border="1">
				<thead>
					<tr>
						<td>考试ID</td>
						<td class="td">考试名称</td>
						<td class="td">开始时间</td>
						<td class="td">结束时间</td>
						<td>自动开始</td>
						<td>未开始</td>
						<td>进行中</td>
						<td>已结束</td>
						<td>手动操作</td>
						<td>学生答案下载</td>
					</tr>
				</thead>
				<tbody id="tbd">

				</tbody>
			</table>
		</form>

	</body>
	<script>
		$(function() {
			getExamDetail();
		})

		function getExamDetail() {
			$.ajax({
				url: "${pageContext.request.contextPath}/TeacherServlet",
				type: "GET",
				data: {
					"method": "listExam"
				},
				dataType: "json",
				success: function(data) {
					var tbody = document.getElementById('tbd');
					for (var i = 0; i < data.length; ++i) {
						var tr = document.createElement('tr');
						tbody.appendChild(tr);
						var id = null;
						for (var k in data[i]) {
							if (k == "id")
								id = data[i][k];
							var td = document.createElement('td');
							tr.appendChild(td);
							var jud = data[i][k];
							var show = null;
							if (jud == 1 || jud == 0) {
								show = jud == 1 ? "√" : "";
							} else {
								show = data[i][k];
							}
							td.innerHTML = show;
						}
						var td = document.createElement("td");
						tr.appendChild(td);
						td.innerHTML = "<a href='javascript:void(0);' onclick='begin(this)' name='" + id +
							"'>开始</a> <a href='javascript:void(0);' onclick='end(this)' name='" + id +
							"'>结束</a> <a href='javascript:void(0);' onclick='del(this)' name='" + id + "'>删除</a>";
						var td = document.createElement("td");
						tr.appendChild(td);
						var form = document.createElement("form");
						form.method="get";
						form.action="TeacherServlet";
						form.innerHTML='<input type="hidden" name="method" value="downloadSubmitFiles" /><input type="hidden" name="id" value="' +
							id + '" /><input type="submit" value="下载" />';
						td.appendChild(form);
					}
				},
				error: function() {
					alert('获取考试信息失败！');
				}
			});
		}

		function begin(obj) {
			var id = $(obj).attr("name");
			$.ajax({
				url: "${pageContext.request.contextPath}/TeacherServlet",
				type: "GET",
				data: {
					"method": "beginExam",
					"id": id
				},
				success: function(data) {
					alert(data);
					window.location.href = "Teacher_Addexam.jsp";
				},
				error: function() {
					alert('开始失败！');
				}
			});
		}

		function end(obj) {
			var id = $(obj).attr("name");
			$.ajax({
				url: "${pageContext.request.contextPath}/TeacherServlet",
				type: "GET",
				data: {
					"method": "endExam",
					"id": id
				},
				success: function(data) {
					window.location.href = "Teacher_Addexam.jsp";
				},
				error: function() {
					alert('结束失败！');
				}
			});
		}

		function del(obj) {
			var id = $(obj).attr("name");
			$.ajax({
				url: "${pageContext.request.contextPath}/TeacherServlet",
				type: "GET",
				data: {
					"method": "deleteExam",
					"id": id
				},
				success: function(data) {
					window.location.href = "Teacher_Addexam.jsp";
				},
				error: function() {
					alert('删除失败！');
				}
			});
		}

		function download(obj) {
			var id = $(obj).attr("name");
			$.ajax({
				url: "${pageContext.request.contextPath}/StudentServlet",
				type: "GET",
				data: {
					"method": "downloadSubmitFiles",
					"id": id
				},
				success: function(data) {
					window.location.href = "Teacher_Addexam.jsp";
				},
				error: function() {
					alert('删除失败！');
				}
			});
		}
	</script>
</html>
